'use strict';
const {
	verifyToken,
	getToken
} = require("login-common")
exports.main = async (event, context) => {
	//event为客户端上传的参数
	const {
		api
	} = event
	const db = uniCloud.database()
	const payload = verifyToken(event.token) // token携带的信息解密出来 得到 openid
	const token = getToken(payload.openid)
	console.log('userToken', payload);
	/*  登录使用账号密码的话，如果payload返回false 证明token已过期
	if(payload == 'false') {  
	    return false
	}*/
	// 用户提交的反馈
	if (api === 'submitFeedback') {
		return await db.collection("feedBack").add({
			openid: event.openid,
			title: event.title,
			content: event.content,
			contentAvatarUrl: event.contentAvatarUrl,
			createTime: Date.now()
		})
	}
	// 更新用户的姓名或头像
	if (api === 'updateAvatar_nicknApi') {
		await db.collection("user").where({
			openid: payload.openid
		}).update({
			nickName: event.nickName,
			avatarUrl: event.avatarUrl
		})
	}
	//  更新用户的主题颜色
	if (api === 'updateColor') {
		await db.collection("user").where({
			openid: payload.openid // 通过openid为索引更新该用户数据
		}).update({
			themeColors: event.color
		})
	}
	// 获取更新的用户信息
	if (api === "getUserInfo") {
		let newUser = await db.collection("user").where({
			openid: payload.openid
		}).get()
		newUser.data[0]['token'] = token
		return newUser
	}
	//返回数据给客户端
	return event
};
